package com.cuitrip.business.login;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.apiservice.UserBusiness;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 7/16.
 */
public class ModifyPasswdActivity extends CustomUiFragmentActivity implements IProxyCallback {
    public static final String HAS_PASSWORD="hasPasswd";

    @Bind(R.id.old_password)
    EditText nowPasswordText;

    @Bind(R.id.new_first_password)
    EditText newFirstPassword;

    @Bind(R.id.new_second_password)
    EditText newSecondPassword;

    @Bind(R.id.password_change_visibility)
    IconTextView togglePwdVisibility;

    @Bind(R.id.old_password_layout)
    LinearLayout oldPasswdLayout;


    private ApiProxy apiProxy;

    private Animation mShakeAnimation;
    private boolean hasPasswd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        hasPasswd = getIntent().getBooleanExtra(HAS_PASSWORD, false);

        super.onCreate(savedInstanceState, R.layout.ct_modify_password);
        apiProxy = new ApiProxy(this);
    }

    @Override
    public void initView() {
        mShakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
        ButterKnife.bind(this);
        nowPasswordText.setTypeface(Typeface.DEFAULT);
        newFirstPassword.setTypeface(Typeface.DEFAULT);
        newSecondPassword.setTypeface(Typeface.DEFAULT);
        oldPasswdLayout.setVisibility(hasPasswd?View.VISIBLE:View.GONE);
        newSecondPassword.setImeOptions(EditorInfo.IME_ACTION_DONE);
        newSecondPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                tryModify();
                return true;
            }
        });
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public void onTopbarRightPress() {
        tryModify();
    }

    @OnClick(R.id.password_change_visibility)
    public void changePwdVisibility() {
        if (togglePwdVisibility.getText().equals(getString(R.string.hide_pwd_icon))) {
            togglePwdVisibility.setText(R.string.show_pwd_icon);
            int selectionEnd = newSecondPassword.getSelectionEnd();
            newFirstPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            newSecondPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            newSecondPassword.setSelection(selectionEnd);
        } else {
            togglePwdVisibility.setText(R.string.hide_pwd_icon);
            int selectionEnd = newSecondPassword.getSelectionEnd();
            newFirstPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            newSecondPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            newSecondPassword.setSelection(selectionEnd);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private boolean verifyText(EditText editText){
        if (TextUtils.isEmpty(editText.getText())){
            editText.startAnimation(mShakeAnimation);
            return false;
        }
        return true;
    }

    public void tryModify() {
        /**
         * 如果有密码，需要判断旧密码，否则不需要
         */
        boolean judgePassword = hasPasswd ? verifyText(nowPasswordText) : true;
        if (judgePassword && verifyText(newFirstPassword) && verifyText(newSecondPassword)) {
            showNoCancelDialog();
            UserBusiness.modifyPassword(apiProxy, nowPasswordText.getText().toString(), newFirstPassword.getText().toString(), newSecondPassword.getText().toString());
        }
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.password_change);
        config.mTopbarRightAreaName = getString(R.string.operation_ok_1);
        config.mRightStyle = CustomUiConfig.AreaStyle.TEXT_STYLE;
        return config;
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response != null && response.code == CtResponseCode.RESULT_OK) {
            hideNoCancelDialog();
            MessageUtils.showToast(R.string.password_change_feedback_suc);
            finish();
        } else {
            hideNoCancelDialog();
            MessageUtils.showToast(response.msg);
        }
        return false;
    }
}
